//
//  HXQRCodeVC.m
//  爱鲜蜂
//
//  Created by 洪东恒 on 16/5/8.
//  Copyright © 2016年 建国. All rights reserved.
//

#import "HXQRCodeVC.h"

#import "HXQRCodeTool.h"

@interface HXQRCodeVC ()

@property (weak, nonatomic) IBOutlet UIView *scanBackView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *toBottom;

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation HXQRCodeVC

-(void)viewDidLoad{

    self.title = @"店铺二维码";
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
        [self beginScanAnimation];
        [self beginScan];
        
    });
    
    NSLog(@"调用吗");
}


// 开始扫描
- (void)beginScan
{
    
    [HXQRCodeTool sharedHXQRCodeTool].isDrawQRCodeRect = YES;
    [[HXQRCodeTool sharedHXQRCodeTool] setInsteretRect:self.scanBackView.frame];
    [[HXQRCodeTool sharedHXQRCodeTool] beginScanInView:self.view result:^(NSArray<NSString *> *resultStrs) {
        
        if (resultStrs != nil) {
            
            //获得二维码数据  弹框  拿到第一条的数据
            [self presentAlertController:resultStrs[0]];
            //关闭动画
            [self stopAnimation];
            
            [[HXQRCodeTool sharedHXQRCodeTool] stopScan];
        }
  
    }];
}

// 开始扫描动画
- (void)beginScanAnimation
{
    self.toBottom.constant = self.scanBackView.frame.size.height;
    [self.view layoutIfNeeded];
    
    
    [UIView animateWithDuration:2 animations:^{
        [UIView setAnimationRepeatCount:CGFLOAT_MAX];
        self.toBottom.constant = - self.scanBackView.frame.size.height;
        [self.view layoutIfNeeded];
        
    }];
}

//获得二维码数据  弹框
-(void)presentAlertController:(NSString *) qRCodeString{

    //绘制边框  弹出二维码信息
    //    NSLog(@"%@",transformObj.stringValue);
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"结果" message:qRCodeString preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //判断是否是网页 跳转网页
        NSURL *url = [NSURL URLWithString:qRCodeString];
        
         if ([[UIApplication sharedApplication] canOpenURL:url]) {
        
             //关闭动画
             [self stopAnimation];
             
             //停止扫描
             [[HXQRCodeTool sharedHXQRCodeTool] stopScan];
        
             // 3.打开网页的URL
            [[UIApplication sharedApplication] openURL:url];
             
        }

        
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //取消就 重新扫描
        [self beginScanAnimation];
        [self beginScan];
        // 移除二维码边框图层
        [[HXQRCodeTool sharedHXQRCodeTool] removeShapLayers];
        
    }];
    
    //添加子控件
    [alertVC addAction:action1];
    [alertVC addAction:action2];
    
    //把警告控制器 表现出来
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

//关闭动画
// 停止动画
-(void)stopAnimation{
    // 动画的本质就是作用在layer层, 所以, 只需要拿到对应的动画layer, 移除就可以
    [self.imageView.layer removeAllAnimations];
    
}

//当控制器消亡的时候  关闭扫描
-(void)dealloc{

    //关闭动画
    [self stopAnimation];
    
    //停止扫描
    [[HXQRCodeTool sharedHXQRCodeTool] stopScan];

}


@end

